import request from '@/utils/request';
import type { ListBaseQuery, ListData, ResBody } from '../common';
import type { PlaceOrder } from '../post-order';
import type {
  DispatchCar,
  DriveCarListQuery,
  DriverCarVo,
  HdwaresDocVo,
  OrderData,
  OrderListQuery,
  ReceivingOrder,
  ReturnOrderDispatchUser,
  ReturnOrderListQuery,
  SjWareDocVo,
  TransferOrderDispatchCar,
  TransferOrderListQuery,
  ZcwaresDocVo
} from './@types';

export * from './@types';

export const getOrderList = (params?: OrderListQuery): Promise<ListData<OrderData>> => request.get('/wuliu/wares/list', { params });

export const getOrderDetail = (id: string): ResBody<OrderData> => request.get(`/wuliu/wares/${id}`);

export const getAreaOrderList = (deptId: string): ResBody<SjWareDocVo[]> => request.get('/wuliu/wares/newlist', { params: { deptId } });

export const getReturnOrderNewList = (deptId: string): ResBody<any> => request.get('/wuliu/wares/newhdlist', { params: { deptId } });

export const getWaitOrderList = async (deptId: any): Promise<SjWareDocVo[]> => {
  const [{ data: areaOrderList }, { data: returnOrderList }] = await Promise.all([getAreaOrderList(deptId), getReturnOrderNewList(deptId)]);

  if (returnOrderList) {
    areaOrderList.push(returnOrderList);
  }
  return areaOrderList;
};

export const getFcOrderList = (params: { carId?: string; deptId?: string }): ResBody<SjWareDocVo[]> =>
  request.get('/wuliu/wares/newfclist', { params });

export interface GetFcOldOrderListQuery extends ListBaseQuery {
  driveCarId?: string;
  firstNum?: number;
}

export const getFcOldOrderList = (params: GetFcOldOrderListQuery): ResBody<SjWareDocVo[]> => request.get('/wuliu/wares/fcoldlist', { params });

export const getDriveCarList = (params?: DriveCarListQuery): Promise<ListData<DriverCarVo>> => request.get('/wuliu/drivecar/list', { params });

export const modifyOrder = (data: PlaceOrder[]): ResBody => request.put('/wuliu/docMx', data);

export const receivingOrder = (data: ReceivingOrder): ResBody => request.put('/wuliu/wares/receiving', data);

export const dispatchCar = (data: DispatchCar): ResBody => request.put('/wuliu/wares/dispatchCar', data);

export const getexportFcFile = (id: string): ResBody => request.post(`/wuliu/wares/getFcPdfData?id=${id}`);
/* 转车单 */
export const getTransferOrderList = (params?: TransferOrderListQuery): Promise<ListData<ZcwaresDocVo>> => request.get('/wuliu/zczb/list', { params });

export const createTransferOrder = (data: PlaceOrder[]): ResBody => request.post('/wuliu/zczb', data);

export const getTransferOrder = (id: string | number): ResBody<ZcwaresDocVo> => request.get(`/wuliu/zczb/${id}`);

export const transferOrderDispatchCar = (data: TransferOrderDispatchCar): ResBody => request.post('/wuliu/zczb/fpzc', data);

/* 回传单 */
export const getReturnOrderList = (params?: ReturnOrderListQuery): Promise<ListData<HdwaresDocVo>> => request.get('/wuliu/hdzb/list', { params });

export const getReturnOrder = (id: string | number): ResBody<HdwaresDocVo> => request.get(`/wuliu/hdzb/${id}`);

export const createReturnOrder = (data: PlaceOrder[]): ResBody => request.post('/wuliu/hdzb', data);

export const returnOrderDispatchUser = (data: ReturnOrderDispatchUser): ResBody => request.post('/wuliu/hdzb/fpr', data);

export const newShrZc = (data: { carId: number; carName: string; mxId: string[] }): ResBody => request.post('/wuliu/wares/newShrZc', data);
